//
//  Network.swift
//  SSNews
//
//  Created by 叶竞泽 on 2017/4/30.
//  Copyright © 2017年 叶竞泽. All rights reserved.
//

import Foundation
import AFNetworking

enum HttpMethod {
    case POST
    case GET
}

class SS_Network: AFHTTPSessionManager {
    
    static let shared: SS_Network = {
        let manager = SS_Network(sessionConfiguration: URLSessionConfiguration.default)
        return manager
    }()
    
    func request(method: HttpMethod, urlString: String, parameters: Any?, progress: ((Progress) -> Void)?, success: ((URLSessionDataTask, Any?) -> Void)?, failure: ((URLSessionDataTask?, Error) -> Void)?) {
        // 显示联网指示器
        UIApplication.shared.isNetworkActivityIndicatorVisible = true
        let success1 = {
            (task: URLSessionDataTask, result: Any?) in
            UIApplication.shared.isNetworkActivityIndicatorVisible = false
            success?(task, result)
        }
        let failure1 = {
            (task: URLSessionDataTask?, error: Error) in
            UIApplication.shared.isNetworkActivityIndicatorVisible = false
            failure?(task, error)
        }
        if method == .GET {
            SS_Network.shared.get(urlString, parameters: parameters, progress: progress, success: success1, failure: failure)
        } else if method == .POST {
            SS_Network.shared.post(urlString, parameters: parameters, progress: progress, success: success, failure: failure1)
        }
    }
}
